﻿using LJSheng.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using EntityFramework.Extensions;
using System.Text;
using System.Data;

namespace LJSheng.Web.Controllers
{
    public class shopController : SController
    {
        public ActionResult mima()
        {
            return View();
        }
        // GET: shop
        public ActionResult index()
        {
            using (EFDB db = new EFDB())
            {
                try
                {
                    Guid gid = Common.LCookie.GetShop(2);
                    //var b = db.ShopList.Where(l => l.gid == gid).FirstOrDefault();
                    //ViewBag.name = b.name;
                    //ViewBag.grade = b.grade;
                    //ViewBag.fl = b.fl;
                    //ViewBag.ewm = LJShengHelper.EWM + b.ewm;
                    //ViewBag.dmz = LJShengHelper.DMZ + b.dmz;
                    //ViewBag.khh = b.khh;
                    //ViewBag.khm = b.khm;
                    //ViewBag.kahao = b.kahao;
                    //ViewBag.qx = b.qx;
                    //ViewBag.sort = b.sort;
                    //Guid ugid = Common.LCookie.GetShop(1);
                    ViewBag.ye = db.LUser.Where(l => l.gid == gid).FirstOrDefault().rmb;
                    //var sw = db.ShopList.Where(l => l.name == "合作升商务").FirstOrDefault();
                    //ViewBag.sw = sw.gid;
                    //ViewBag.swpay = sw.payrmb;
                    //Guid usergid = Common.LCookie.GetShop(1);
                    //会员消费
                    var o = db.LOrder.Where(l => l.usergid == gid).ToList();
                    if (o != null)
                    {
                        ViewBag.wdxf = o.Sum(l => l.user_cash);
                    }
                    ////我的账户
                    //var p = db.Pay.Where(l => l.usergid == usergid && l.pay_status == 1).ToList();
                    //if (p != null)
                    //{
                    //    ViewBag.wdzh = p.Where(l => l.lx == 3).Sum(l => l.rmb);
                    //    ViewBag.wdfl = p.Where(l => l.lx != 3).Sum(l => l.rmb);
                    //}
                }
                catch
                {
                    Common.LCookie.DelALLCookie();
                }
                return View();
            }
        }
        public ActionResult list()
        {
            return View();
        }
        #region 商家资料
        public ActionResult sjzl()
        {
            using (EFDB db = new EFDB())
            {
                Guid gid = Common.LCookie.GetShop(2);
                var b = db.ShopList.Where(l => l.gid == gid).FirstOrDefault();
                ViewBag.name = b.name;
                ViewBag.picture = LJShengHelper.Shop + b.picture;
                ViewBag.rmb = b.rmb;
                ViewBag.explain = b.explain;
                ViewBag.introduce = b.introduce;
                ViewBag.province = b.province;
                ViewBag.city = b.city;
                ViewBag.area = b.area;
                ViewBag.address = b.address;
                ViewBag.qx = b.qx;
                ViewBag.grade = b.grade;
                ViewBag.account = b.account;
                ViewBag.xfx = b.xfx;
                ViewBag.xfd = b.xfd;
                ViewBag.tese = b.tese;
                ViewBag.zhekou = b.zhekou;
                ViewBag.fl = b.fl;
                ViewBag.flgid = b.flgid;
                ViewBag.lxfs = b.lxfs;
                return View(db.CPFL.ToList());
            }
        }

        /// <summary>
        /// 更新商家资料
        /// </summary>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult upshop()
        {
            using (EFDB db = new EFDB())
            {
                string name = Request.Form["name"];
                string explain = Request.Form["explain"];
                string introduce = Request.Form["introduce"];
                string province = Request.Form["province"];
                string city = Request.Form["city"];
                string area = Request.Form["area"];
                string address = Request.Form["address"];
                int xfx = Int32.Parse(Request.Form["xfx"]);
                int xfd = Int32.Parse(Request.Form["xfd"]);
                string tese = Request.Form["tese"];
                string zhekou = Request.Form["zhekou"];
                string lxfs = Request.Form["lxfs"];
                Guid flgid = Guid.Parse(Request.Form["flgid"]);
                Guid gid = Common.LCookie.GetShop(2);
                var b = db.ShopList.Where(l => l.gid == gid).FirstOrDefault();
                b.name = name;
                b.flgid = flgid;
                b.explain = explain;
                b.introduce = introduce;
                b.province = province;
                b.city = city;
                b.area = area;
                b.address = address;
                b.xfx = xfx;
                b.xfd = xfd;
                b.tese = tese;
                b.zhekou = zhekou;
                b.lxfs = lxfs;
                b.show = 1;
                if (db.SaveChanges() == 1)
                {
                    return Helper.Redirect("操作成功", "history.go(-1);", "恭喜你,商家资料修改成功!");
                }
                else
                {
                    return Helper.Redirect("操作失败", "history.go(-1);", "更新失败,请检查录入的数据!");
                }

            }
        }

        /// <summary>
        /// 客满
        /// </summary>
        [HttpPost]
        public JsonResult cpkm(int sort)
        {
            Guid gid = Common.LCookie.GetShop(2);
            if (gid != null)
            {
                using (EFDB db = new EFDB())
                {
                    if (db.ShopList.Where(l => l.gid == gid).Update(l => new ShopList { sort = sort == 0 ? 1 : 0 }) == 1)
                    {
                        return Json(new AjaxResult(sort == 0 ? 1 : 0));
                    }
                    else
                    {
                        return Json(new AjaxResult(300, "失败"));
                    }
                }
            }
            else
            {
                return Json(new AjaxResult(300, "非法参数"));
            }
        }
        #endregion

        #region 订单管理
        public ActionResult ddlist()
        {
            if (Request.Form["daochu"] == "daochu")
            {
                using (EFDB db = new EFDB())
                {
                    string stime = Request.Form["stime"];
                    string etime = Request.Form["etime"];
                    string order_no = Request.Form["order_no"];
                    string account = Request.Form["account"];
                    string name = Request.Form["name"];
                    int pay_type = string.IsNullOrEmpty(Request.Form["pay_type"]) ? 0 : Int32.Parse(Request.Form["pay_type"]);
                    int pay_status = string.IsNullOrEmpty(Request.Form["pay_status"]) ? 0 : Int32.Parse(Request.Form["pay_status"]);
                    Guid? usergid = null;
                    if (!string.IsNullOrEmpty(Request.Form["usergid"]))
                    {
                        usergid = Guid.Parse(Request.Form["usergid"]);
                    }
                    Guid? buygid = null;
                    if (!string.IsNullOrEmpty(Request.Form["buygid"]))
                    {
                        buygid = Guid.Parse(Request.Form["buygid"]);
                    }
                    var b = db.LOrder.Select(l => new
                    {
                        l.gid,
                        l.addtime,
                        l.usergid,
                        l.buygid,
                        l.order_no,
                        l.trade_no,
                        l.pay_type,
                        l.pay_time,
                        l.pay_status,
                        l.ptf,
                        l.hyfwf,
                        l.tgf,
                        l.amount,
                        l.rmb,
                        l.user_cash,
                        l.remarks,
                        l.coupon_number,
                        l.zhekou,
                        db.LUser.Where(u => u.gid == l.usergid).FirstOrDefault().account,
                        username = db.LUser.Where(u => u.gid == l.usergid).FirstOrDefault().real_name,
                        db.ShopList.Where(s => s.gid == l.buygid).FirstOrDefault().name
                    }).AsQueryable();
                    if (usergid != null)
                    {
                        b = b.Where(l => l.usergid == usergid);
                    }
                    if (buygid != null)
                    {
                        b = b.Where(l => l.buygid == buygid);
                    }
                    if (!string.IsNullOrEmpty(order_no))
                    {
                        b = b.Where(l => (l.order_no.Contains(order_no) || l.trade_no.Contains(order_no)));
                    }
                    if (pay_type != 0)
                    {
                        b = b.Where(l => l.pay_type == pay_type);
                    }
                    if (pay_status != 0)
                    {
                        b = b.Where(l => l.pay_status == pay_status);
                    }
                    if (!string.IsNullOrEmpty(account))
                    {
                        b = b.Where(l => l.account.Contains(account));
                    }
                    if (!string.IsNullOrEmpty(name))
                    {
                        b = b.Where(l => l.name.Contains(name));
                    }
                    //时间查询
                    if (!string.IsNullOrEmpty(stime) || !string.IsNullOrEmpty(etime))
                    {
                        DateTime? st = null;
                        DateTime? et = null;
                        if (!string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(etime))
                        {
                            st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                            et = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 23:59:59");
                        }
                        else if (string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                        {
                            st = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 00:00:00");
                            et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                        }
                        else
                        {
                            st = DateTime.Parse(stime);
                            et = DateTime.Parse(etime);
                        }
                        b = b.Where(l => l.addtime >= st && l.addtime <= et);
                    }
                    //获取list数据
                    var list = b.OrderByDescending(l => l.addtime).ToList();

                    //创建Excel文件的对象
                    NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
                    //添加一个sheet
                    NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1");

                    //给sheet1添加第一行的头部标题
                    NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
                    row1.CreateCell(0).SetCellValue("系统订单号");
                    row1.CreateCell(1).SetCellValue("网上银行订单");
                    row1.CreateCell(2).SetCellValue("会员帐号");
                    row1.CreateCell(3).SetCellValue("商家名称");
                    row1.CreateCell(4).SetCellValue("支付状态");
                    row1.CreateCell(5).SetCellValue("平台/会员/推广费");
                    row1.CreateCell(6).SetCellValue("原价");
                    row1.CreateCell(7).SetCellValue("支付金额");
                    //将数据逐步写入sheet1各个行
                    int i = 0;
                    for (i = 0; i < list.Count; i++)
                    {
                        NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);
                        rowtemp.CreateCell(0).SetCellValue(list[i].order_no);
                        rowtemp.CreateCell(1).SetCellValue(list[i].trade_no);
                        rowtemp.CreateCell(2).SetCellValue(list[i].account + "(" + list[i].username + ")");
                        rowtemp.CreateCell(3).SetCellValue(list[i].name);
                        rowtemp.CreateCell(4).SetCellValue(list[i].pay_status == 1 ? "已付" : "未付");
                        rowtemp.CreateCell(5).SetCellValue(list[i].ptf.ToString() + "/" + list[i].hyfwf.ToString() + "/" + list[i].tgf.ToString() + "/");
                        rowtemp.CreateCell(6).SetCellValue(list[i].amount.ToString());
                        rowtemp.CreateCell(7).SetCellValue(list[i].rmb.ToString());
                    }
                    NPOI.SS.UserModel.IRow rowend = sheet1.CreateRow(i + 1);
                    //统计
                    decimal parame1 = b.Where(l => l.pay_status == 1).Count() > 0 ? b.Where(l => l.pay_status == 1).Sum(l => l.rmb) : 0;
                    rowend.CreateCell(1).SetCellValue("当前查询条件总收入=" + parame1);
                    // 写入到客户端 
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    book.Write(ms);
                    ms.Seek(0, SeekOrigin.Begin);
                    return File(ms, "application/vnd.ms-excel", "订单.xls");
                }
            }
            else
            {
                return View();
            }
        }

        [HttpPost]
        public ActionResult aorderlist()
        {
            string json = "";
            using (StreamReader sr = new StreamReader(Request.InputStream))
            {
                json = HttpUtility.UrlDecode(sr.ReadLine()); //Server.UrlDecode
            }
            //解析参数
            JObject paramJson = JsonConvert.DeserializeObject(json) as JObject;
            using (EFDB db = new EFDB())
            {
                string stime = paramJson["stime"].ToString();
                string etime = paramJson["etime"].ToString();
                string order_no = paramJson["order_no"].ToString();
                //string account = paramJson["account"].ToString();
                string name = paramJson["name"].ToString();
                string kami = paramJson["kami"].ToString();
                string post = paramJson["post"].ToString();
                //int pay_type = string.IsNullOrEmpty(paramJson["pay_type"].ToString()) ? 0 : Int32.Parse(paramJson["pay_type"].ToString());
                int pay_status = string.IsNullOrEmpty(paramJson["pay_status"].ToString()) ? 0 : Int32.Parse(paramJson["pay_status"].ToString());
                Guid usergid = LJSheng.Common.LCookie.GetUser();
                var b = db.LOrder.Where(l=>l.usergid== usergid).Select(l => new
                {
                    l.gid,
                    l.addtime,
                    l.usergid,
                    l.buygid,
                    l.order_no,
                    l.trade_no,
                    l.pay_type,
                    l.pay_time,
                    l.pay_status,
                    l.ptf,
                    l.hyfwf,
                    l.tgf,
                    l.amount,
                    l.rmb,
                    l.user_cash,
                    l.remarks,
                    l.coupon_number,
                    l.zhekou,
                    l.num,
                    l.tianshu,
                    db.LUser.Where(u => u.gid == l.usergid).FirstOrDefault().account,
                    username = db.LUser.Where(u => u.gid == l.usergid).FirstOrDefault().real_name,
                    db.ShopList.Where(s => s.gid == l.buygid).FirstOrDefault().name
                }).AsQueryable();
                //if (usergid != null)
                //{
                //    b = b.Where(l => l.usergid == usergid);
                //}
                if (!string.IsNullOrEmpty(order_no))
                {
                    b = b.Where(l => (l.order_no.Contains(order_no) || l.trade_no.Contains(order_no)));
                }
                //if (pay_type != 0)
                //{
                //    b = b.Where(l => l.pay_type == pay_type);
                //}
                if (pay_status != 0)
                {
                    b = b.Where(l => l.pay_status == pay_status);
                }
                //if (!string.IsNullOrEmpty(account))
                //{
                //    b = b.Where(l => l.account.Contains(account));
                //}
                if (!string.IsNullOrEmpty(name))
                {
                    b = b.Where(l => l.name.Contains(name));
                }
                if (!string.IsNullOrEmpty(kami))
                {
                    b = b.Where(l => l.coupon_number.Contains(kami));
                }
                if (!string.IsNullOrEmpty(post))
                {
                    b = b.Where(l => l.remarks.Contains(post));
                }
                //时间查询
                if (!string.IsNullOrEmpty(stime) || !string.IsNullOrEmpty(etime))
                {
                    DateTime? st = null;
                    DateTime? et = null;
                    if (!string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else if (string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else
                    {
                        st = DateTime.Parse(stime);
                        et = DateTime.Parse(etime);
                    }
                    b = b.Where(l => l.pay_time >= st && l.pay_time <= et);
                }
                //开始分页
                int pageindex = string.IsNullOrEmpty(paramJson["pageindex"].ToString()) ? 1 : Int32.Parse(paramJson["pageindex"].ToString());
                int pagesize = 10;//每页显示的数量
                int count = b.Count();//总行数
                decimal parame1 = b.Where(l => l.pay_status == 1).Count() > 0 ? b.Where(l => l.pay_status == 1).Sum(l => l.rmb) : 0;
                return Json(new AjaxResult(new
                {
                    parame1,
                    count,
                    pages = Math.Ceiling((double)count / (double)pagesize),
                    pageindex,
                    list = b.OrderByDescending(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList()
                }));
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        [HttpPost]
        public JsonResult orderdelete(Guid gid)
        {
            if (gid != null)
            {
                using (EFDB db = new EFDB())
                {
                    if (db.LOrder.Where(l => l.gid == gid).Delete() == 1)
                    {
                        return Json(new AjaxResult("成功"));
                    }
                    else
                    {
                        return Json(new AjaxResult(300, "失败"));
                    }
                }
            }
            else
            {
                return Json(new AjaxResult(300, "非法参数"));
            }
        }
        #endregion

        #region 会员消费
        public ActionResult hyxf()
        {
            return View();
        }
        /// <summary>
        /// 获取商家订单列表
        /// </summary>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        [HttpPost]
        public ActionResult orderlist()
        {
            string json = "";
            using (StreamReader sr = new StreamReader(Request.InputStream))
            {
                json = HttpUtility.UrlDecode(sr.ReadLine()); //Server.UrlDecode
            }
            //解析参数
            JObject paramJson = JsonConvert.DeserializeObject(json) as JObject;
            //string stime = paramJson["stime"].ToString();
            //string etime = paramJson["etime"].ToString();
            string order_no = paramJson["order_no"].ToString();
            string account = paramJson["account"].ToString();
            string name = paramJson["name"].ToString();
            string kami = paramJson["kami"].ToString();
            string post = paramJson["post"].ToString();
            int pay_status = string.IsNullOrEmpty(paramJson["pay_status"].ToString()) ? 0 : Int32.Parse(paramJson["pay_status"].ToString());
            using (EFDB db = new EFDB())
            {
                Guid buygid = Common.LCookie.GetShop(2);
                var b = db.LOrder.Where(l => l.usergid == buygid).GroupJoin(db.LUser,
                    x => x.usergid,
                    y => y.gid,
                    (x, y) => new
                    {
                        x.gid,
                        x.addtime,
                        x.order_no,
                        x.amount,
                        x.rmb,
                        x.user_cash,
                        x.pay_status,
                        x.pay_time,
                        x.buygid,
                        x.ptf,
                        x.hyfwf,
                        x.tgf,
                        x.remarks,
                        x.coupon_number,
                        x.zhekou,
                        x.num,
                        x.tianshu,
                        y.FirstOrDefault().account,
                        y.FirstOrDefault().real_name,
                        db.ShopList.Where(s => s.gid == x.buygid).FirstOrDefault().name
                    }).AsQueryable();
                if (!string.IsNullOrEmpty(order_no))
                {
                    b = b.Where(l => (l.order_no.Contains(order_no)));
                }
                if (pay_status != 0)
                {
                    b = b.Where(l => l.pay_status == pay_status);
                }
                if (!string.IsNullOrEmpty(account))
                {
                    b = b.Where(l => l.account.Contains(account));
                }
                if (!string.IsNullOrEmpty(name))
                {
                    b = b.Where(l => l.name.Contains(name));
                }
                if (!string.IsNullOrEmpty(kami))
                {
                    b = b.Where(l => l.coupon_number.Contains(kami));
                }
                if (!string.IsNullOrEmpty(post))
                {
                    b = b.Where(l => l.remarks.Contains(post));
                }
                //时间查询
                //if (!string.IsNullOrEmpty(stime) || !string.IsNullOrEmpty(etime))
                //{
                //    DateTime? st = null;
                //    DateTime? et = null;
                //    if (!string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(etime))
                //    {
                //        st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                //        et = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 23:59:59");
                //    }
                //    else if (string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                //    {
                //        st = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 00:00:00");
                //        et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                //    }
                //    else
                //    {
                //        st = DateTime.Parse(stime);
                //        et = DateTime.Parse(etime);
                //    }
                //    b = b.Where(l => l.addtime >= st && l.addtime <= et);
                //}
                //开始分页
                int pageindex = string.IsNullOrEmpty(paramJson["pageindex"].ToString()) ? 1 : Int32.Parse(paramJson["pageindex"].ToString());
                int pagesize = 10;//每页显示的数量
                int count = b.Count();//总行数
                return Json(new AjaxResult(new
                {
                    pages = Math.Ceiling((double)count / (double)pagesize),
                    pageindex,
                    list = b.OrderByDescending(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList()
                }));
            }
        }

        public ActionResult pay()
        {
            using (EFDB db = new EFDB())
            {
                Guid shopgid = Common.LCookie.GetShop(2);
                return View(db.zhekou.Where(l => l.shopgid == shopgid).ToList());
            }
        }
        #endregion

        #region 我的会员
        public ActionResult wdtj()
        {
            Guid gid = Common.LCookie.GetUser();
            using (EFDB db = new EFDB())
            {
                var b = db.LUser.Where(l => l.gid == gid).Select(l => new
                {
                    l.number,
                    l.usergid
                }).FirstOrDefault();
                ViewBag.number = b.number;
                if (b.usergid != null && b.usergid != Guid.Parse("00000000-0000-0000-0000-000000000000"))
                {
                    var u = db.LUser.Where(l => l.gid == b.usergid).Select(l => new
                    {
                        l.number,
                        l.account,
                        l.real_name
                    }).FirstOrDefault();
                    ViewBag.account = u.account;
                    ViewBag.real_name = u.real_name;
                    ViewBag.n = u.number;
                }
                else
                {
                    ViewBag.account = "顶级";
                    ViewBag.real_name = "用户";
                    ViewBag.n = 0;
                }
                return View();
            }
        }

        /// <summary>
        /// 我的会员列表
        /// </summary>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        [HttpPost]
        public ActionResult userlist()
        {
            string json = "";
            using (StreamReader sr = new StreamReader(Request.InputStream))
            {
                json = HttpUtility.UrlDecode(sr.ReadLine()); //Server.UrlDecode
            }
            //解析参数
            JObject paramJson = JsonConvert.DeserializeObject(json) as JObject;
            using (EFDB db = new EFDB())
            {
                Guid usergid = Common.LCookie.GetShop(1);
                var b = db.LUser.Where(l => l.usergid == usergid).Select(l => new
                {
                    l.account,
                    l.real_name,
                    l.addtime,
                    l.gid,
                    l.number,
                    grade = l.lx == 3 ? db.ShopList.Where(s => s.usergid == l.gid).FirstOrDefault().grade : l.grade
                }).AsQueryable();
                //开始分页
                int pageindex = string.IsNullOrEmpty(paramJson["pageindex"].ToString()) ? 1 : Int32.Parse(paramJson["pageindex"].ToString());
                int pagesize = 10;//每页显示的数量
                int count = b.Count();//总行数
                return Json(new AjaxResult(new
                {
                    pages = Math.Ceiling((double)count / (double)pagesize),
                     pageindex,
                    list = b.OrderByDescending(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList()
                }));
            }
        }
        #endregion

        #region 我的账户
        public ActionResult wdzh()
        {
            return View();
        }
        /// <summary>
        /// 会员提现列表
        /// </summary>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        [HttpPost]
        public ActionResult txlist()
        {
            string json = "";
            using (StreamReader sr = new StreamReader(Request.InputStream))
            {
                json = HttpUtility.UrlDecode(sr.ReadLine()); //Server.UrlDecode
            }
            //解析参数
            JObject paramJson = JsonConvert.DeserializeObject(json) as JObject;
            using (EFDB db = new EFDB())
            {
                Guid usergid = Common.LCookie.GetShop(1);
                var b = db.Pay.Where(l => l.usergid == usergid).Select(l => new { l.pay_status, l.rmb, l.addtime, l.remarks }).AsQueryable();
                //开始分页
                int pageindex = string.IsNullOrEmpty(paramJson["pageindex"].ToString()) ? 1 : Int32.Parse(paramJson["pageindex"].ToString());
                int pagesize = 10;//每页显示的数量
                int count = b.Count();//总行数
                return Json(new AjaxResult(new
                {
                    pages = Math.Ceiling((double)count / (double)pagesize),
                     pageindex,
                    list = b.OrderByDescending(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList()
                }));
            }
        }
        #endregion

        #region 后台
        // 后台
        public ActionResult houtai()
        {
            return View();
        }
        // 菜单
        //[Authorize(Roles = "admins")]
        public ActionResult caidan()
        {
            return View();
        }
        // 头部
        public ActionResult toubu()
        {
            return View();
        }

        /// <summary>
        /// 头部数据
        /// </summary>
        [HttpPost]
        public JsonResult tb()
        {
            string ugid = LJSheng.Common.LCookie.GetUser().ToString();
            StringBuilder sb = new StringBuilder("SELECT TOP 1 ");
            sb.Append("(SELECT COUNT(pay_status) FROM LOrder WHERE pay_status=2 AND usergid='"+ ugid + "') AS dd,");
            sb.Append("(SELECT COUNT(pay_status) FROM LOrder WHERE pay_status=1 AND usergid='" + ugid + "') AS pay,");
            sb.Append("(SELECT SUM(user_cash) FROM LOrder WHERE usergid='" + ugid + "') AS rmb,");
            sb.Append("(SELECT rmb FROM LUser WHERE gid='" + ugid + "') AS ye");
            //sb.Append("(SELECT COUNT(pay_status) FROM LOrder WHERE pay_status=1 AND addtime >= '" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + " 00:00:00' AND addtime <= '" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + " 23:59:59" + "') AS ddzr,");
            sb.Append(" FROM LOrder");
            DataTable dt = DbHelperSQL.Query(sb.ToString()).Tables[0];
            if (dt.Rows.Count > 0)
            {
                
                    return Json(new AjaxResult(new
                    {
                        dd = dt.Rows[0]["dd"].ToString(),
                        pay = dt.Rows[0]["pay"].ToString(),
                        rmb = dt.Rows[0]["rmb"].ToString(),
                        ye = dt.Rows[0]["ye"].ToString()

                    }));

            }
            else
            {
                using (EFDB db = new EFDB())
                {
                    Guid gid = LJSheng.Common.LCookie.GetUser();
                    return Json(new AjaxResult(new
                    {
                        dd = "0",
                        pay = "0",
                        rmb = "0",
                        ye = db.LUser.Where(l=>l.gid== gid).FirstOrDefault().rmb
                    }));
                }
            }
        }
        public ActionResult zcpay()
        {
            try
            {
                JObject linjiansheng = JsonConvert.DeserializeObject(Common.DESRSA.DESDeljsheng(Common.LCookie.GetCookie("linjiansheng"))) as JObject;
                Guid gid = Guid.Parse(linjiansheng["gid"].ToString());
                using (EFDB db = new EFDB())
                {
                    var b = db.LUser.Where(l => l.gid == gid).FirstOrDefault();
                    ViewBag.rmb = b.rmb;
                    ViewBag.zk = b.zk;
                    ViewBag.qx = b.qx;
                }
                return View();
            }
            catch
            {
                return Helper.Redirect("账户异常！", "/denglu.aspx?lx=" + Request.QueryString["lx"], "请重新登录！");
            }
        }

        /// <summary>
        /// 注册套餐
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult zctc()
        {
            string json = "";
            using (StreamReader sr = new StreamReader(Request.InputStream))
            {
                json = HttpUtility.UrlDecode(sr.ReadLine()); //Server.UrlDecode
            }
            //解析参数
            JObject paramJson = JsonConvert.DeserializeObject(json) as JObject;
            Guid usergid = LJSheng.Common.LCookie.GetUser();
            using (EFDB db = new EFDB())
            {
                var b = db.ShopList.Where(l => l.show == 1).GroupJoin(db.TC.Where(j => j.usergid == usergid),
                    x => x.gid,
                    y => y.shopgid,
                    (x, y) => new
                    {
                        x.gid,
                        x.sort,
                        x.addtime,
                        x.flgid,
                        x.name,
                        x.xfx,
                        tcgid = y.FirstOrDefault() != null ? y.FirstOrDefault().gid.ToString() : "",
                        rmb = y.FirstOrDefault() != null ? y.FirstOrDefault().rmb : 0
                    }).AsQueryable();

                //开始分页
                int pageindex = string.IsNullOrEmpty(paramJson["pageindex"].ToString()) ? 1 : Int32.Parse(paramJson["pageindex"].ToString());
                int pagesize = 1984;//每页显示的数量
                int count = b.Count();//总行数
                return Json(new AjaxResult(new
                {
                    pages = Math.Ceiling((double)count / (double)pagesize),
                    pageindex,
                    list = b.Where(l => l.rmb > 0).OrderByDescending(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList()
                }));
            }
        }
        #endregion
    }
}